<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>角色管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="名称/关键字" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="enterQuery()"></el-input>
                <el-button @click="conditionQuery()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="角色名称" align="center"></el-table-column>
                <el-table-column prop="keyword" label="角色关键字" align="center"></el-table-column>
                <el-table-column prop="description" label="角色描述" align="center"></el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- TODO: 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataAddForm" status-icon :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="20">
                                            <el-form-item label="角色名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="20">
                                            <el-form-item label="角色关键字" prop="keyword">
                                                <el-input v-model="formData.keyword"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>

                            </el-tab-pane>
                            <el-tab-pane label="权限信息" name="second">
                                <!--                                TODO: 新增 permission table-->
                                <el-table
                                        ref="addPermissionFilterTable"
                                        :data="permissionTableData"
                                        style="width: 100%;height: 30px"
                                        height="240"
                                        border
                                        :row-class-name="tableRowClassName"
                                        @selection-change="handlePermissionSelectionChange"
                                >
                                    <el-table-column
                                            type="selection"
                                            width="50">
                                    </el-table-column>
                                    <el-table-column
                                            prop="name"
                                            label="权限名称"
                                            width="110"
                                            height="40"
                                            sortable
                                    >
                                    </el-table-column>
                                    <el-table-column
                                            prop="keyword"
                                            label="权限关键字"
                                            width="180"
                                            height="40"
                                            sortable
                                    >
                                    </el-table-column>
                                    <el-table-column
                                            prop="description"
                                            label="权限描述"
                                            fit
                                            height="40"
                                    >
                                    </el-table-column>
                                </el-table>
                            </el-tab-pane>

                            <el-tab-pane label="菜单信息" name="third">
                                <!--                                TODO: 新增 menu table-->
                                <el-table
                                        ref="addMeanFilterTable"
                                        :data="menuTableData"
                                        style="width: 100%;height: 30px"
                                        height="240"
                                        border
                                        :row-class-name="tableRowClassName"
                                        @selection-change="handleMenuSelectionChange"
                                >
                                    <el-table-column
                                            type="selection"
                                            width="50">
                                    </el-table-column>
                                    <el-table-column
                                            prop="name"
                                            label="菜单名称"
                                            width="110"
                                            height="40"
                                            sortable
                                    >
                                    </el-table-column>
                                    <el-table-column
                                            prop="linkUrl"
                                            label="菜单url"
                                            width="180"
                                            height="40"
                                            sortable
                                    >
                                    </el-table-column>
                                    <el-table-column
                                            prop="description"
                                            label="菜单描述"
                                            fit
                                            height="40"
                                    >
                                    </el-table-column>
                                </el-table>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- TODO: 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataEditForm" status-icon :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="20">
                                            <el-form-item label="角色名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="20">
                                            <el-form-item label="角色关键字" prop="keyword">
                                                <el-input v-model="formData.keyword"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="权限信息" name="second">

                                <!--                                TODO: 编辑 permission table-->
                                <el-table
                                        ref="editFilterTable"
                                        :data="permissionData"
                                        style="width: 100%;height: 30px"
                                        height="240"
                                        border
                                        :row-class-name="tableRowClassName">
                                    <el-table-column
                                            prop="name"
                                            label="权限名称"
                                            width="110"
                                            height="40"
                                            sortable
                                    >
                                    </el-table-column>
                                    <el-table-column
                                            prop="keyword"
                                            label="权限关键字"
                                            width="180"
                                            height="40"
                                            sortable
                                    >
                                    </el-table-column>
                                    <el-table-column
                                            prop="description"
                                            label="权限描述"
                                            fit
                                            height="40"
                                    >
                                    </el-table-column>
                                </el-table>
                                <div style="text-align: right">
                                    <el-button type="primary" icon="el-icon-edit" circle
                                               @click="handlePermissionListDialogVisibleEdit()"></el-button>
                                </div>

                            </el-tab-pane>

                            <el-tab-pane label="菜单信息" name="third">

                                <!--                                TODO: 编辑 menu table-->
                                <el-table
                                        ref="editFilterTable"
                                        :data="menuData"
                                        style="width: 100%;height: 30px"
                                        height="240"
                                        border
                                        :row-class-name="tableRowClassName">
                                    <el-table-column
                                            prop="name"
                                            label="菜单名称"
                                            width="110"
                                            height="40"
                                            sortable
                                    >
                                    </el-table-column>
                                    <el-table-column
                                            prop="linkUrl"
                                            label="菜单url"
                                            width="180"
                                            height="40"
                                            sortable
                                    >
                                    </el-table-column>
                                    <el-table-column
                                            prop="description"
                                            label="菜单描述"
                                            fit
                                            height="40"
                                    >
                                    </el-table-column>
                                </el-table>
                                <div style="text-align: right">
                                    <el-button type="primary" icon="el-icon-edit" circle
                                               @click="handleMenuListDialogVisibleEdit()"></el-button>
                                </div>

                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>

                <!--                TODO: permission列表编辑弹窗 穿梭框-->
                <el-dialog title="编辑权限" :visible.sync="permissionListDialogVisibleEdit">
                    <div style="text-align: center">
                        <el-transfer
                                :left-default-checked="transfer.emptyArr"
                                :right-default-checked="transfer.emptyArr"
                                filterable
                                :filter-method="filterMethod"
                                filter-placeholder="请输入权限名称"
                                style="text-align: left;display: inline-block"
                                :data="transfer.permissionSelectableData"
                                v-model="transfer.permissionSelectedData"
                                :titles="['可选', '已选']"
                                @change="handleChange"
                                :props="{key: 'id',label: 'name'}"
                                ref="permissionTransfer"
                        >
                            <div style="text-align: right" slot="right-footer">
                                <el-button @click="submitChangePermissionForRole"
                                           icon="el-icon-check" circle
                                           type="warning">
                                </el-button>
                            </div>

                        </el-transfer>
                    </div>

                </el-dialog>


                <!--                TODO: menu列表编辑弹窗 穿梭框-->
                <el-dialog title="编辑菜单" :visible.sync="menuListDialogVisibleEdit">
                    <div style="text-align: center">
                        <el-transfer
                                :left-default-checked="transfer.emptyArr"
                                :right-default-checked="transfer.emptyArr"
                                filterable
                                :filter-method="filterMethod"
                                filter-placeholder="请输入菜单名称"
                                style="text-align: left;display: inline-block"
                                :data="transfer.menuSelectableData"
                                v-model="transfer.menuSelectedData"
                                :titles="['可选', '已选']"
                                @change="handleChange"
                                :props="{key: 'id',label: 'name'}"
                                ref="menuTransfer"
                        >
                            <div style="text-align: right" slot="right-footer">
                                <el-button @click="submitChangeMenuForRole"
                                           icon="el-icon-check" circle
                                           type="warning">
                                </el-button>
                            </div>

                        </el-transfer>
                    </div>

                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>

    //TODO: vue
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            permissionTableData: [],//新增表单中对应的权限列表数据
            menuTableData: [],//新增表单中对应的菜单列表数据
            permissionData: [],//编辑表单中对应的权限列表数据
            menuData: [],//编辑表单中对应的菜单列表数据

            currentRowInfo: {}, //当前行信息

            //TODO: 编辑 穿梭框数据容器
            transfer: {
                emptyArr: [], //空数组
                permissionSelectableData: [],//对应的权限可选列表数据
                permissionSelectedData: [],//对应的权限已选列表数据
                initPermissionForRoleIds: [], //对应权限初始id列表
                ChangePermissionForRoleIdsForDelete: [], //对应的权限需要删除的id 列表

                menuSelectableData: [],//对应的菜单可选列表数据
                menuSelectedData: [],//对应的菜单已选列表数据
                initMenuForRoleIds: [], //对应菜单初始id列表
                ChangeMenuForRoleIdsForDelete: [], //对应的菜单需要删除的id 列表
            },
            // TODO-----------------------------------------

            permissionIds: [],//新增和编辑表单中角色对应的权限复选框，基于双向绑定可以进行回显和数据提交
            menuIds: [],//新增和编辑表单中角色对应的菜单复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
            permissionListDialogVisibleEdit: false,//控制permission列表编辑窗口显示/隐藏
            menuListDialogVisibleEdit: false,//控制menu列表编辑窗口显示/隐藏

            //TODO: 校验规则
            rules: {//校验规则
                name: [
                    {required: true, message: '名称为必填项', trigger: 'blur'},
                ],
                keyword: [
                    {required: true, message: '关键字为必填项', trigger: 'blur'},
                ]
            }
        },
        //vue 被创建就分页查询
        created() {
            this.findPage();
        },

        //TODO: 监听dialog的开闭状态并执行异步校验
        watch: {
            dialogFormVisible(val) {
                !val && setTimeout(() => {
                    this.$refs['dataAddForm'].clearValidate();
                    this.$refs.addPermissionFilterTable.clearSort();
                }, 0);
            },
            dialogFormVisible4Edit(val) {
                !val && setTimeout(() => {
                    this.$refs['dataEditForm'].clearValidate();
                    this.$refs.editFilterTable.clearSort();
                }, 0);
            },
            permissionListDialogVisibleEdit(val) {
                val && setTimeout(() => {
                    //查询所有角色数据
                    this.queryAllPermission();

                }, 0);
                !val && setTimeout(() => {
                    //    清除穿梭框搜索内容,清除所有clearQuery 需要v2.2+
                    //左边
                    this.$refs.permissionTransfer.$children["0"]._data.query = '';
                    // 清空右边搜索
                    this.$refs.permissionTransfer.$children["3"]._data.query = '';

                    //    清空穿梭框选择状态   失败!
                    this.transfer.emptyArr.length = 0;
                }, 0);
            },
            menuListDialogVisibleEdit(val) {
                val && setTimeout(() => {
                    //查询所有角色数据
                    this.queryAllMenu();

                }, 0);
                !val && setTimeout(() => {
                    //    清除穿梭框搜索内容,清除所有clearQuery 需要v2.2+
                    //左边
                    this.$refs.menuTransfer.$children["0"]._data.query = '';
                    // 清空右边搜索
                    this.$refs.menuTransfer.$children["3"]._data.query = '';

                    //    清空穿梭框选择状态   失败!
                    this.transfer.emptyArr.length = 0;
                }, 0);
            }
        },

        methods: {
            //权限不足提示
            showMessage(r){
                if(r == 'Error: Request failed with status code 403'){
                    //权限不足
                    this.$message.error('无访问权限');
                }else{
                    this.$message.error('未知错误');
                }
            },
            enterQuery() {
                this.findPage();
            },

            //TODO:  重置表单
            resetForm() {
                //    等于空完事
                this.formData = {};

                //    每次弹窗显示第一个tabs
                this.activeName = "first";
                //    每次弹窗取消勾选权限,清空id数组
                this.permissionIds = [];

                //    每次弹窗取消勾选菜单,清空id数组
                this.menuIds = [];
            },

            // TODO: 弹出添加窗口
            handleCreate() {
                //        每次弹出清空表单数据
                this.resetForm();
                //    dialog visible.sync true完事
                this.dialogFormVisible = true;

                // //清除校验规则
                // this.$refs['dataAddForm'].clearValidate();

                //    弹出即查询所有permission
                this.queryAllPermission();

                //    弹出即查询所有menu
                this.queryAllMenu();


            },

            //TODO: 查询所有Permission
            queryAllPermission() {
                axios.post("/permission/queryAllPermission.do").then((res) => {
                    if (res.data.flag) {
                        this.permissionTableData = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            },

            //TODO: 查询所有menu
            queryAllMenu() {
                axios.post("/menu/findAll.do").then((res) => {
                    if (res.data.flag) {
                        this.menuTableData = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            },

            //TODO: permission列表row样式
            tableRowClassName({row, rowIndex}) {
                if (rowIndex % 2 == 1) {
                    return 'warning-row';
                } else if (rowIndex % 2 == 0) {
                    return 'success-row';
                }
                return '';
            },

            //TODO: 添加
            handleAdd() {

                //表单校验是否合法在添加
                //语法this.$refs["表单ref"].validate((valid) =>{})
                this.$refs["dataAddForm"].validate((valid) => {
                    //    给予合法提示
                    if (valid) {
                        //判断是否添加了角色
                        if (this.permissionIds.length == 0 || this.menuIds.length==0) {
                            this.$message.error("请至少添加一个权限和一个菜单");
                            return;
                        }
                        //  发送axios请求  将表单对象与permissionIds/menuIds数组作为参数传递
                        let param = {
                            formData: this.formData,
                            permissionIds: this.permissionIds,
                            menuIds: this.menuIds
                        };
                        axios.post("/role/addRole.do", param).then((res) => {
                            //    判断数据是否添加成功并给予消息提示
                            if (res.data.flag) {

                                this.$message.success(res.data.message);
                                //    添加成功关闭弹出框
                                this.dialogFormVisible = false;
                                //    成功后查询
                                this.findPage();
                            } else {
                                this.$message.error(res.data.message);
                            }

                        }).catch((r)=>{
                            this.showMessage(r)
                        })

                    } else {
                        this.$message.error("表单数据不合法噢~~,重新输入吧")
                    }
                });

            },

            //TODO: 分页查询
            findPage() {
                //json数据参数,name与bean属性要一致
                let param = {
                    //当前页
                    currentPage: this.pagination.currentPage,
                    //页码
                    pageSize: this.pagination.pageSize,
                    //    查询条件
                    queryString: this.pagination.queryString
                };

                //    发送axios请求查询分页数据与总信息数
                axios.post("/role/queryPage.do", param).then((res) => {
                    //    判断flag
                    if (res.data.flag) {
                        //    为页面数据数组赋值
                        this.dataList = res.data.data.rows;
                        //    为页面信息总数赋值
                        this.pagination.total = res.data.data.total;
                    } else {
                        //错误提示信息
                        this.$message.error(res.data.message);
                    }
                }).catch((r)=>{
                    this.showMessage(r)
                })

            },

            //TODO: 关键字查询
            conditionQuery() {
                this.pagination.currentPage = 1;
                this.findPage();
            },

            //TODO: 切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },

            //TODO: 为新增role时permission id 数组赋值
            handlePermissionSelectionChange(val) {
                // console.log(val);
                this.permissionIds.length = 0;
                for (let i = 0; i < val.length; i++) {
                    this.permissionIds.push(val[i].id)
                }
                // console.log(this.permissionIds);
            },

            //TODO: 为新增role时menu id 数组赋值
            handleMenuSelectionChange(val) {
                // console.log(val);
                this.menuIds.length = 0;
                for (let i = 0; i < val.length; i++) {
                    this.menuIds.push(val[i].id)
                }
                // console.log(this.permissionIds);
            },

            //TODO:  弹出编辑窗口
            handleUpdate(row) {

                //不知道为什么首次打开要查询两次
                //查询所有权限数据
                this.queryAllPermission();
                //查询所有菜单数据
                this.queryAllMenu();

                //    每次弹窗显示第一个tabs
                this.activeName = "first";
                //visible.sync = true
                this.dialogFormVisible4Edit = true;

                // //清除校验规则
                // this.$refs['dataEditForm'].clearValidate();

                //    数据回显 row就是封装了当前行的所有数据,直接赋值表单对象
                this.formData = JSON.parse(JSON.stringify(row));

                //   查询对应权限
                this.queryPermissionForRole(row);
                //查询对应菜单
                this.queryMenuForRole(row);


            },

            //    TODO: 查询对应权限
            queryPermissionForRole(row) {

                //将row提取
                this.currentRowInfo = row;

                let param = {id: row.id};
                axios.post("/permission/queryPermissionForRole.do", param).then((res) => {
                    //判断并给出相应提示消息
                    if (res.data.flag) {
                        //填充数据
                        this.permissionData = res.data.data;
                        /*this.$message.success(res.data.message);*/
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            },

            //    TODO: 查询对应菜单
            queryMenuForRole(row) {

                //将row提取
                this.currentRowInfo = row;

                let param = {id: row.id};
                axios.post("/menu/queryMenuForRole.do", param).then((res) => {
                    //判断并给出相应提示消息
                    if (res.data.flag) {
                        //填充数据
                        this.menuData = res.data.data;
                       /* this.$message.success(res.data.message);*/
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
            },

            // TODO: 弹出对应permission列表编辑穿梭框
            handlePermissionListDialogVisibleEdit() {

                //弹出
                this.permissionListDialogVisibleEdit = true;

                // console.log(this.transfer.emptyArr);

                //遍历取出id与name重新赋值给数组
                let arrCollection = this.forEachData(this.permissionTableData, this.permissionData);
                //可选穿梭框列表赋值
                this.transfer.permissionSelectableData = arrCollection.arrALL;

                //已选穿梭框列表赋值
                this.transfer.permissionSelectedData = arrCollection.arrSelected;

                //将已选穿梭框数组记录初始值
                this.transfer.initPermissionForRoleIds = [].concat(arrCollection.arrSelected);

            },

            // TODO: 弹出对应permission列表编辑穿梭框
            handleMenuListDialogVisibleEdit() {

                //弹出
                this.menuListDialogVisibleEdit = true;

                // console.log(this.transfer.emptyArr);

                //遍历取出id与name重新赋值给数组
                let arrCollection = this.forEachData(this.menuTableData, this.menuData);
                //可选穿梭框列表赋值
                this.transfer.menuSelectableData = arrCollection.arrALL;

                //已选穿梭框列表赋值
                this.transfer.menuSelectedData = arrCollection.arrSelected;

                //将已选穿梭框数组记录初始值
                this.transfer.initMenuForRoleIds = [].concat(arrCollection.arrSelected);

            },

            //TODO: 穿梭框搜索过滤方法...
            filterMethod(query, val) {
                // console.log(query)
                // console.log(permission)
                return val.name.indexOf(query) > -1;
            },

            //TODO: 穿梭框change方法
            handleChange(value, direction, movedKeys) {
                // console.log(value);// 所有key
                // console.log(movedKeys);// 移动的key
                // console.log(direction); //方向
                //可以通过direction回调right/left 来进行操作，right：把数据移到右边，left把数据移到左边
                if (direction === "right") {
                    //    根据id添加
                    // console.log(this.transfer.permissionSelectedData);
                    // console.log(this.transfer.initPermissionForRoleIds);

                }
                if (direction === "left") {
                    //根据id删除
                    // console.log(this.transfer.permissionSelectedData);
                    // console.log(this.transfer.initPermissionForRoleIds);

                }

            },

            // TODO: 遍历取出id与name重新赋值给数组
            forEachData(allSelectData, selectedData) {
                //重新生成对象数组
                let arrALL = new Array();
                let arrSelected = new Array();
                for (let i = 0; i < allSelectData.length; i++) {
                    arrALL.push({
                        id: allSelectData[i].id,
                        name: (i + 1) + ".\t--\t" + allSelectData[i].name,
                    })
                }

                for (let i = 0; i < selectedData.length; i++) {
                    arrSelected.push(selectedData[i].id);
                }


                //返回对象
                return {
                    arrALL: arrALL,
                    arrSelected: arrSelected
                };

            },

            //TODO: 修改role对应permission列表按钮
            submitChangePermissionForRole() {

                if (this.transfer.permissionSelectedData.length == 0) {
                    this.$message.error("请至少添加一个权限");
                    return;
                }

                // 重置id数组
                this.transfer.ChangePermissionForRoleIdsForDelete.length = 0;


                // console.log(this.transfer.initPermissionForRoleIds);

                //    处理最终发送后台的permissionId数据
                let param = this.finalPermissionIdsHandle();

                // console.log(param);
                //发送请求更新对应角色数据
                axios.post("/role/updatePermissionForRoleByDetail.do", param).then((res) => {
                    if (res.data.flag) {
                        this.$notify({
                            title: '提示',
                            message: res.data.message,
                            duration: 2000,
                            type: 'success'
                        });
                        //再次查询对应角色
                        this.queryPermissionForRole(this.currentRowInfo);
                        //成功后关闭窗口
                        this.permissionListDialogVisibleEdit = false;
                    } else {
                        this.$notify.error({
                            title: '提示',
                            message: res.data.message,
                            duration: 3000,
                        });
                    }
                });

            },


            //TODO: 修改role对应menu列表按钮
            submitChangeMenuForRole() {

                if (this.transfer.menuSelectedData.length == 0) {
                    this.$message.error("请至少添加一个权限");
                    return;
                }

                // 重置id数组
                this.transfer.ChangeMenuForRoleIdsForDelete.length = 0;


                // console.log(this.transfer.initPermissionForRoleIds);

                //    处理最终发送后台的permissionId数据
                let param = this.finalMenuIdsHandle();

                // console.log(param);
                //发送请求更新对应角色数据
                axios.post("/role/updateMenuForRoleByDetail.do", param).then((res) => {
                    if (res.data.flag) {
                        this.$notify({
                            title: '提示',
                            message: res.data.message,
                            duration: 2000,
                            type: 'success'
                        });
                        //再次查询对应角色
                        this.queryMenuForRole(this.currentRowInfo);
                        //成功后关闭窗口
                        this.menuListDialogVisibleEdit = false;
                    } else {
                        this.$notify.error({
                            title: '提示',
                            message: res.data.message,
                            duration: 3000,
                        });
                    }
                });

            },
            //TODO: 包装数据,返回最终需要删除与添加的 permission id数组,当前role id
            finalPermissionIdsHandle() {
                //去重
                //外循环初始数组
                for (let i = 0; i < this.transfer.initPermissionForRoleIds.length; i++) {
                    let flag = false;
                    //内循环已选数组
                    for (let j = 0; j < this.transfer.permissionSelectedData.length; j++) {
                        //如果出现相同元素,删除并改变标记
                        if (this.transfer.initPermissionForRoleIds[i] == this.transfer.permissionSelectedData[j]) {
                            flag = true;
                            //删除
                            this.transfer.permissionSelectedData.splice(j, 1);
                            j--;
                        }
                    }
                    //如果外循环一次没有相同元素,添加需要删除的permission id
                    if (!flag) {
                        this.transfer.ChangePermissionForRoleIdsForDelete.push(this.transfer.initPermissionForRoleIds[i])
                    }

                }

                //返回包装后数据
                return {
                    deleteIds: this.transfer.ChangePermissionForRoleIdsForDelete,
                    // 重定义数组记录需要添加的id
                    addIds: [].concat(this.transfer.permissionSelectedData),
                    currentRowId: this.currentRowInfo.id
                };
            },


            //TODO: 包装数据,返回最终需要删除与添加的 menu id数组,当前role id
            finalMenuIdsHandle() {
                //去重
                //外循环初始数组
                for (let i = 0; i < this.transfer.initMenuForRoleIds.length; i++) {
                    let flag = false;
                    //内循环已选数组
                    for (let j = 0; j < this.transfer.menuSelectedData.length; j++) {
                        //如果出现相同元素,删除并改变标记
                        if (this.transfer.initMenuForRoleIds[i] == this.transfer.menuSelectedData[j]) {
                            flag = true;
                            //删除
                            this.transfer.menuSelectedData.splice(j, 1);
                            j--;
                        }
                    }
                    //如果外循环一次没有相同元素,添加需要删除的menu id
                    if (!flag) {
                        this.transfer.ChangeMenuForRoleIdsForDelete.push(this.transfer.initMenuForRoleIds[i])
                    }

                }

                //返回包装后数据
                return {
                    deleteIds: this.transfer.ChangeMenuForRoleIdsForDelete,
                    // 重定义数组记录需要添加的id
                    addIds: [].concat(this.transfer.menuSelectedData),
                    currentRowId: this.currentRowInfo.id
                };
            },
            //TODO: 编辑
            handleEdit() {
                //    表单校验
                this.$refs["dataEditForm"].validate((valid) => {
                    if (valid) {
                        //判断是否存在对应角色
                        if (this.permissionData.length == 0) {
                            this.$message.error("请至少添加一个角色");
                            return;
                        }
                        //    发送请求,根据id修改数据,数据回显时id已存在formData中了
                        axios.post("/role/updateRole.do", this.formData).then((res) => {
                            //    判断是否修改成功
                            if (res.data.flag) {
                                this.$message.success(res.data.message);
                                //    成功后关闭窗口
                                this.dialogFormVisible4Edit = false;
                                //    成功后查询
                                this.findPage();
                            } else {
                                this.$message.error(res.data.message);
                            }
                        }).catch((r)=>{
                            this.showMessage(r)
                        });
                    } else {
                        this.$message.error("改的啥玩意??,重改!")
                    }
                });
            },

            //TODO:  删除
            handleDelete(row) {
                //提示框
                this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //判断当前页数据是否为最后一条并修改当前页码
                    if (this.dataList.length == 1 && this.pagination.currentPage > 1) {
                        this.pagination.currentPage = this.pagination.currentPage - 1;
                    }
                    //    传入id完事
                    let param = {id: row.id};
                    axios.post("/role/deleteRole.do", param).then((res) => {
                        //    判断是否删除成功
                        if (res.data.flag) {
                            this.$message.success(res.data.message);
                            //    删完查询
                            this.findPage();
                        } else {
                            this.$message.error(res.data.message);
                        }
                    }).catch((r)=>{
                        this.showMessage(r)
                    });
                }).catch(() => {
                    this.$message('已取消删除');
                });

            }
        }

    })
</script>
</html>
